package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.entity.Knowledge;
import com.hl.sfzy.service.KnowledgeService;
import com.hl.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 1. @ClassDescription:
 * 2. @ClassName KnowledgeController
 * 3. @Description TODO
 * 4. @Author: YuKunXu
 * 5. @Date: 2024年01月11日 21:50
 * 6. @Project: sfzy
 * 7. @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {
    @Autowired
    private KnowledgeService knowledgeService;
    @PostMapping("/add")
    public Result add(@RequestBody Knowledge knowledge) throws Exception {
        knowledgeService.add(knowledge);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody Knowledge knowledge) {
        knowledgeService.edit(knowledge);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        knowledgeService.remove(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/remove")
    public Result removeBatch(Integer[] ids) {
        System.out.println(ids);
        knowledgeService.removeBatch(ids);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @GetMapping("/find/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(knowledgeService.findById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/find")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                           @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                           Knowledge knowledge) {
        return Result.success(knowledgeService.findPage(pageNum, pageSize, knowledge));
    }
}